import c_extends from "../extends";
var throttleType;
(function (throttleType) {
    throttleType[throttleType["time"] = 0] = "time";
    throttleType[throttleType["frequency"] = 1] = "frequency";
})(throttleType || (throttleType = {}));
class Throttle {
    constructor(fun, { type = throttleType.time, duration = 250, frequency = 10 }) {
        this.onCount = false;
        this.count = 0;
        this.isFirst = true;
        this.duration = 0;
        this.frequency = 0;
        this.type = throttleType.time;
        // 时间类型节流阀
        this.timeTypeThrottle = () => {
            if (this.isFirst) {
                this.isFirst = false;
                return this.callBack();
            }
            if (this.onCount)
                return;
            this.onCount = true;
            this.timeId = setTimeout(() => {
                this.callBack();
                this.onCount = false;
                clearTimeout(this.timeId);
            }, this.duration);
        };
        // 频率类型节流阀
        this.frequencyTypeThrottle = () => {
            if (this.isFirst) {
                this.isFirst = false;
                this.callBack();
                return;
            }
            if (this.count < this.frequency) {
                this.count++;
                return;
            }
            this.callBack();
            this.count = 0;
        };
        c_extends(this, { type, duration, frequency });
        this.callBack = fun;
    }
}
function throttle(fun, options) {
    let thr = new Throttle(fun, options ? options : {});
    if (thr.type === throttleType.time) {
        return thr.timeTypeThrottle;
    }
    else {
        return thr.frequencyTypeThrottle;
    }
}
export { Throttle };
export default throttle;
